package com.cn.web.controller.api;

import cn.com.allunion.common.AllUnionResult;
import cn.com.allunion.common.page.QueryResult;
import cn.com.allunion.sms.pojo.Smsoperator;
import cn.com.allunion.sms.service.mybatis.SmsoperatorService;
import com.cn.web.bean.PageViewResult;
import com.cn.web.bean.OperatorResult;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 运营商控制器
 * Created by YangJie on 2016/4/20.
 */
@Controller
@RequestMapping("/operator")
public class OperatorController {
    private static final Logger LOGGER = LoggerFactory.getLogger(OperatorController.class) ;

    @Autowired
    SmsoperatorService smsoperatorService ;

    @Value("${maxResult}")
    int pageSize ;

    /**
     * 添加运营商
     * @param operatorName 运营商名称
     * @param operatorCode 运营商编码
     * @param status 状态码，1表示开，0表示关
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Object create(@RequestParam("operatorName") String operatorName,
                         @RequestParam("operatorCode")String operatorCode,
                         @RequestParam("status")String status) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("request params[operatorName: {}, operatorCode: {}, status: {}]", operatorName, operatorCode, status);
        }

        // 参数校验
        if(StringUtils.isBlank(operatorName) || StringUtils.isBlank(operatorCode) || StringUtils.isBlank(status)) {
            return AllUnionResult.build(HttpStatus.BAD_REQUEST.value(), "参数异常") ;
        }
        if (!("0".equals(status) || "1".equals(status))) {
            return AllUnionResult.build(HttpStatus.BAD_REQUEST.value(), "状态码参数异常") ;
        }

        // 判断 运营商名称和代码的唯一性
        Smsoperator smsoperator = smsoperatorService.findbyName(operatorName);
        if (null != smsoperator) {
            return AllUnionResult.build(HttpStatus.INTERNAL_SERVER_ERROR.value(), "运营商名称已经存在");
        }
        smsoperator = smsoperatorService.findOperatorObj(operatorCode) ;
        if (null != smsoperator) {
            return AllUnionResult.build(HttpStatus.INTERNAL_SERVER_ERROR.value(), "运营商代码已经存在");
        }

        // 保存到数据库
        String result = smsoperatorService.addNewOperator(operatorName, 20, operatorCode, status);

        if ("success".equals(result)) {
            return AllUnionResult.ok() ;
        }

        return AllUnionResult.build(HttpStatus.INTERNAL_SERVER_ERROR.value(), "系统内部处理出现错误") ;
    }

    /**
     * 修改运营商
     * @param operatorName 运营商名称
     * @param operatorCode 运营商编码
     * @param status 状态
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Object update(@RequestParam("operatorId") String operatorId,
                         @RequestParam("operatorName") String operatorName,
                         @RequestParam("operatorCode")String operatorCode,
                         @RequestParam("status")String status) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("request params[operatorId: {}, operatorName: {}, operatorCode: {}, status: {}]",
                    operatorId, operatorName, operatorCode, status);
        }

        // 参数校验
        if(StringUtils.isBlank(operatorId) || StringUtils.isBlank(operatorName)
                || StringUtils.isBlank(operatorCode) || StringUtils.isBlank(status)) {
            return AllUnionResult.build(HttpStatus.BAD_REQUEST.value(), "参数异常") ;
        }
        if (!("0".equals(status) || "1".equals(status))) {
            return AllUnionResult.build(HttpStatus.BAD_REQUEST.value(), "状态码参数异常") ;
        }

        String result = smsoperatorService.udpateOperator(operatorId, operatorName, 20, operatorCode, status);

        if ("success".equals(result)) {
            return AllUnionResult.ok() ;
        }

        return AllUnionResult.build(HttpStatus.INTERNAL_SERVER_ERROR.value(), "系统内部处理出现错误") ;
    }

    /**
     * 根据运营商id获取运营商信息
     * @param operatorId 运营商id
     */
    @RequestMapping(value = "/get/{operatorId}", method = RequestMethod.GET)
    @ResponseBody
    public Object get(@PathVariable("operatorId") String operatorId) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("request params[operatorId: {}]", operatorId);
        }
        if (StringUtils.isBlank(operatorId)) {
            return AllUnionResult.build(HttpStatus.BAD_REQUEST.value(), "运营商ID参数异常");
        }

        // 根据id获取运营商信息
        Smsoperator smsoperator = smsoperatorService.findById(operatorId);

        if (null == smsoperator) {
            return AllUnionResult.build(HttpStatus.NOT_FOUND.value(), "无此运营商ID对应的信息");
        }

        // 转化为返回的结果集对象
        OperatorResult result = OperatorResult.convert(smsoperator);

        return AllUnionResult.ok(result) ;
    }

    /**
     * 分页获取运营商信息列表
     * @param pageCurrent 当前页码，从1开始
     */
    @RequestMapping(value = "/scroll/{pageCurrent}", method = RequestMethod.GET)
    @ResponseBody
    public Object scroll(@PathVariable("pageCurrent") int pageCurrent) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("request params[pageCurrent: {}]", pageCurrent);
        }
        if (pageCurrent <= 0) {
            return AllUnionResult.build(HttpStatus.BAD_REQUEST.value(), "当前页码参数异常");
        }

        QueryResult<Smsoperator> queryResult = smsoperatorService.getScrollData(pageCurrent, pageSize) ;

        PageViewResult<OperatorResult> pageView = new PageViewResult<>(pageSize, pageCurrent) ;
        pageView.setQueryResult(OperatorResult.convert(queryResult));

        AllUnionResult result = AllUnionResult.ok(pageView) ;
        return result ;
    }

    /**
     * 获取全部运营商信息列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Object list() {
        List<Smsoperator> smsoperators = smsoperatorService.findAll();
        AllUnionResult result = AllUnionResult.ok(OperatorResult.convert(smsoperators)) ;
        return result ;
    }
}
